package com.hfzy.ihk.web.userCenter.biz;

import com.hfzy.ihk.facade.userCenter.entity.Role;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import com.hfzy.ihk.web.userCenter.mapper.RoleHasPrivMapper;
import com.hfzy.ihk.web.userCenter.mapper.RoleMapper;
import com.hfzy.ihk.web.userCenter.vo.RoleDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by YANFA on 2019/2/13.
 */
@Component
@Slf4j
public class RoleBiz {

    @Autowired
    RoleMapper roleMapper ;

    @Autowired
    RoleHasPrivMapper roleHasPrivMapper;

    /**
     * 保存权限，同时保存关系
     * @param roleDto
     */
    @Transactional
    public void saveRole(RoleDto roleDto) {
        Role role = roleDto;
        List<Long> privIds = roleDto.getPrivIds();

        if (role.getId() != null) {// 修改
            updateRole(role, privIds);
        } else {// 新增
            saveRole(role, privIds);
        }
    }

    /**
     * 更新
     * @param role
     * @param privIds
     */
    public void updateRole(Role role ,List<Long> privIds){
        Role r = roleMapper.getRole(role.getRoleName());
        if (r != null&&role.getId()!=r.getId()) {
            throw new IllegalArgumentException(role.getRoleName() + "已存在");
        }

        UserInfo userInfo = UserUtil.getLoginUser();
        if(userInfo!=null){
            role.setModTime(new Date());
        }

        roleMapper.updateById(role);

        //先删除原来的关系，在重新生成新的关系
        roleHasPrivMapper.deleteRoleHasPriv(role.getId().longValue());

        if (!CollectionUtils.isEmpty(privIds)) {
            roleMapper.saveRoleHasPriv(role.getId().longValue(), privIds);
        }

        log.info("更新角色{}",role.getRoleName());
    }

    /**
     * 保存
     * @param role
     * @param privIds
     */
    public void saveRole(Role role ,List<Long> privIds){
        Role r = roleMapper.getRole(role.getRoleName());
        if (r != null) {
            throw new IllegalArgumentException(role.getRoleName() + "已存在");
        }
        UserInfo userInfo = UserUtil.getLoginUser();
        if(userInfo!=null){
            role.setCreateTime(new Date());
            role.setCreateUserId(userInfo.getId());
        }

        roleMapper.insert(role);
        //保存权限和角色的关系
        if (!CollectionUtils.isEmpty(privIds)) {
            roleMapper.saveRoleHasPriv(role.getId().longValue(), privIds);
        }

        log.info("新增角色{}",role.getRoleName());

    }

    public List<String> getRoleHasPrivByRole(Long roleId){
        return roleHasPrivMapper.getRoleHasPrivByRoleId(roleId);
    }


    @Transactional
    public void deleteRole(Long id) {

        //删除权限角色关系表
        roleHasPrivMapper.deleteRoleHasPriv(id);

        roleMapper.deleteUserHasRoleByRoleId(id);
        //删除角色表
        roleMapper.deleteById(id);


        log.debug("删除角色id:{}", id);
    }
}
